﻿
/* 
 * Since 2008 - 2012
 */
package com.hh.bss.customer.controller;

import static com.hh.bss.util.extjs.JsonHelper.outJson;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;

import com.hh.bss.auth.client.OnlineManager;
import com.hh.bss.auth.client.OnlineUser;
import com.hh.bss.base.BaseSpringController;
import com.hh.bss.common.page.Page;
import com.hh.bss.common.page.PageRequest;
import com.hh.bss.customer.entity.Company;
import com.hh.bss.customer.entity.Customer;
import com.hh.bss.customer.entity.CustomerBd;
import com.hh.bss.customer.service.CustomerBdService;
import com.hh.bss.customer.vo.query.CustomerBdQuery;
import com.hh.bss.util.DateHelper;
import com.hh.bss.util.StringHelper;
import com.hh.bss.util.extjs.ExtJsPageHelper;
import com.hh.bss.util.extjs.ListRange;


@Controller
public class CustomerBdController extends BaseSpringController{
	//默认多列排序,example: username desc,createTime asc
	protected static final String DEFAULT_SORT_COLUMNS = null; 
	
	private CustomerBdService customerBdService;
	
	private CustomerBd customerBd;
	java.lang.Integer id = null;
	private String[] items;

	
	
	/** 通过spring自动注入 */
	public void setCustomerBdServiceImpl(CustomerBdService service) {
		this.customerBdService = service;
	}	
	
	
	
	public void setId(java.lang.Integer val) {
		this.id = val;
	}

	public void setItems(String[] items) {
		this.items = items;
	}



	/**
	 * ExtGrid使用
	 * 列表
	 * @throws IOException
	 */
	public void list(HttpServletRequest request,HttpServletResponse response,CustomerBdQuery customerBdQuery) throws IOException
	{
		PageRequest<Map> pr = ExtJsPageHelper.createPageRequestForExtJs(request, DEFAULT_SORT_COLUMNS);
		Page page = customerBdService.findPage(customerBdQuery);
		
		List<CustomerBd> CustomerBdlist = (List) page.getResult();
		ListRange<CustomerBd> resultList = new ListRange<CustomerBd>();
		resultList.setRows(CustomerBdlist);
		resultList.setTotal(page.getTotalCount());
		resultList.setMessage("ok");
		resultList.setSuccess(true);
		outJson(response,resultList);
	}
	public String selectBdCustomerPage(HttpServletRequest request, HttpServletResponse response) throws IOException {	
		return "forward:/jsp/customer/customerBd/selectBdCustomerPage.jsp";	
	}
	/**
	 * extGrid保存
	 * @throws IOException
	 */
	public void save(HttpServletRequest request,HttpServletResponse response,CustomerBd customerBd) throws IOException
	{
		Map<String, Object> result = new HashMap<String, Object>();
		try
		{
                        //设置创建用户和创建时间
			com.hh.bss.auth.client.OnlineManager onlineManager=super.getOnlineManager(request, response);
			com.hh.bss.auth.client.OnlineUser onlineUser=null;
			if (onlineManager!=null){
					 onlineUser=onlineManager.getOnlineUser();
					if (onlineUser!=null) {
						customerBd.setOnlineUser(onlineUser);
						customerBd.setBdAccount(onlineUser.getUsername());	
					}
					if (onlineUser!=null&&com.hh.bss.util.BeanUtils.isExistsProperty(CustomerBd.class,"creator")) {
						try{com.hh.bss.util.BeanUtils.setProperty(customerBd,"creator",onlineUser.getUsername());}catch(Exception ex){}
					}
				}
							
			if (com.hh.bss.util.BeanUtils.isExistsProperty(CustomerBd.class,"createDt")) {
					try{com.hh.bss.util.BeanUtils.setProperty(customerBd,"createDt",new Date());}catch(Exception ex){}
				}

			customerBd.setBdStatus(new Integer(0));
			customerBd.setExpiredD(DateHelper.add(new Date(), DateHelper.DAY, 90));
					
			customerBdService.save(customerBd);
			result.put("success", true);
			result.put("msg", "添加成功!");
		}
		catch (Exception e)
		{
			result.put("failure", true);
			result.put("msg", e.getMessage());
			e.printStackTrace();
		}
		outJson(response,result);
	}
	public void companysave(HttpServletRequest request,HttpServletResponse response) throws IOException
	{
		Map<String, Object> result = new HashMap<String, Object>();
		try
		{
			String companyIds=request.getParameter("companyIds");
			if(companyIds !=null && !"".equals(companyIds)){
				String [] companyIdArr = companyIds.split(",");
				//if(companyIdArr.length>1){
					for(String companyId : companyIdArr){
						if (StringHelper.parseInteger(companyId, 0)==0){
							result.put("failure", true);
							result.put("msg", "参数不对");
							outJson(response,result);
							return;
						}
						CustomerBd customerBd=new CustomerBd();
			                        //设置创建用户和创建时间
						OnlineManager onlineManager=super.getOnlineManager(request, response);
						OnlineUser onlineUser=null;
						if (onlineManager!=null){
								 onlineUser=onlineManager.getOnlineUser();
								if (onlineUser!=null) {
									customerBd.setOnlineUser(onlineUser);
									customerBd.setBdAccount(onlineUser.getUsername());	
								}
								if (onlineUser!=null&&com.hh.bss.util.BeanUtils.isExistsProperty(CustomerBd.class,"creator")) {
									try{com.hh.bss.util.BeanUtils.setProperty(customerBd,"creator",onlineUser.getUsername());}catch(Exception ex){}
								}
							}
										
						if (com.hh.bss.util.BeanUtils.isExistsProperty(CustomerBd.class,"createDt")) {
								try{com.hh.bss.util.BeanUtils.setProperty(customerBd,"createDt",new Date());}catch(Exception ex){}
							}

											
						customerBdService.save(customerBd,StringHelper.parseInteger(companyId, 0));
					}
					
					result.put("success", true);
					result.put("msg", "添加成功!");
				//}
			}
			

			
		}
		catch (Exception e)
		{
			result.put("failure", true);
			result.put("msg", e.getMessage());
			e.printStackTrace();
		}
		outJson(response,result);
	}
	public void comapnybdcustomersave(HttpServletRequest request,HttpServletResponse response) throws IOException
	{
	    //
		Map<String, Object> result = new HashMap<String, Object>();
		Company company=new Company(); 
		Customer customer=new Customer();
		try {
			 //设置创建用户和创建时间
			com.hh.bss.auth.client.OnlineManager onlineManager=super.getOnlineManager(request, response);
			if (onlineManager!=null){
					com.hh.bss.auth.client.OnlineUser onlineUser=onlineManager.getOnlineUser();
					if (onlineUser!=null&&com.hh.bss.util.BeanUtils.isExistsProperty(Customer.class,"creator")) {
						company.setCreateDt(new Date());
						customer.setCreateDt(new Date());
						company.setCreator(onlineUser.getUsername());
						customer.setCreator(onlineUser.getUsername());						
					}
			}
			bind(request,company);
			bind(request,customer);
			customerBdService.addComapnyBdCustomer(company, customer);
			result.put("success", true);
			result.put("msg", "添加成功!");
		}catch (Exception e)
		{
			result.put("failure", true);
			result.put("msg", e.getMessage());
			e.printStackTrace();
		}
		outJson(response,result);
		
	}
	
	/**
	 * extGrid修改
	 * @throws IOException
	 */
	public void update(HttpServletRequest request,HttpServletResponse response,CustomerBd customerBd) throws IOException
	{
		Map<String, Object> result = new HashMap<String, Object>();
		try
		{
                        CustomerBd oldcustomerBd = (CustomerBd)customerBdService.getById(customerBd.getId());
                         
			bind(request,oldcustomerBd);
                        //设置修改用户和修改时间 
                        com.hh.bss.auth.client.OnlineManager onlineManager=super.getOnlineManager(request, response);
			if (onlineManager!=null){
					com.hh.bss.auth.client.OnlineUser onlineUser=onlineManager.getOnlineUser();
					if (onlineUser!=null&&com.hh.bss.util.BeanUtils.isExistsProperty(CustomerBd.class,"modifier")) {
						try{com.hh.bss.util.BeanUtils.setProperty(oldcustomerBd,"modifier",onlineUser.getUsername());}catch(Exception ex){}
					}
				}
							
			if (com.hh.bss.util.BeanUtils.isExistsProperty(CustomerBd.class,"modifiedDt")) {
					try{com.hh.bss.util.BeanUtils.setProperty(oldcustomerBd,"modifiedDt",new Date());}catch(Exception ex){}
				}


			customerBdService.update(oldcustomerBd);
			result.put("success", true);
			result.put("msg", "修改成功!");
		}
		catch (Exception e)
		{
			result.put("failure", true);
			result.put("msg", e.getMessage());
			e.printStackTrace();
		}
		outJson(response,result);
	}
	
	/**
	 * extGrid删除
	 * @throws IOException
	 */
	public void delete(HttpServletRequest request,HttpServletResponse response) throws IOException
	{
		String ids = request.getParameter("ids");
		String[] idarray = ids.split(",");
		Map<String, Object> result = new HashMap<String, Object>();
		try
		{
			for (int i = 0; i < idarray.length; i++)
			{
				java.lang.Integer id = new java.lang.Integer((String)idarray[i]);
				customerBdService.removeById(id);
			}
			result.put("success", true);
			result.put("msg", "删除成功");
		}
		catch (Exception e)
		{
			result.put("failure", true);
			result.put("msg", e.getMessage());
			e.printStackTrace();
		}
		outJson(response,result);
	}
	
}
